package live.jialing.sip.base.service;

import live.jialing.core.data.LinkType;
import live.jialing.core.data.jpa.CustomJpaRepository;
import live.jialing.core.service.BaseDomainService;
import live.jialing.core.service.GeneralService;
import live.jialing.data.util.SpecificationUtil;
import live.jialing.sip.base.domain.Child;
import live.jialing.sip.base.domain.Student;
import live.jialing.sip.base.repository.StudentRepository;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Map;

/**
 * 学生
 */
@Service
public class StudentService extends BaseDomainService<Student, Long> implements GeneralService<Student, Long> {

    private static Logger logger = LoggerFactory.getLogger(StudentService.class);

    @Resource
    private StudentRepository studentRepository;

    @Resource
    private ChildService childService;

    @Override
    public CustomJpaRepository getRepository() {
        return this.studentRepository;
    }

    public Specification<Student> buildSpecification(java.lang.Class<Student> classz, Map<String, Object> searchParams, LinkType type) {

        return SpecificationUtil.buildSpecification(classz, searchParams, type);
    }

    /**
     * 新增学生，级联新增孩子
     * @param student
     * @return
     * @see live.jialing.sip.base.domain.Child
     */
    @Override
    public Student add(Student student) {

        if (student == null) {
            return null;
        }

        Child child = student.getChild();
        childService.add(student.getChild());

        //
        student.setChild(child);
        return super.add(student);
    }

    /**
     * 批量新增学生，级联新增孩子
     * @param students
     * @see live.jialing.sip.base.domain.Child
     */
    @Override
    public void batchAdd(Iterable<Student> students) {
        super.batchAdd(students);
    }
}
